package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.mapper.*;
import com.example.mybatisplus.model.domain.*;
import com.example.mybatisplus.model.dto.RoomAdjustForm;
import com.example.mybatisplus.service.LeaderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzy
 * @since 2024-06-24
 */
@Service
public class LeaderServiceImpl extends ServiceImpl<LeaderMapper, Leader> implements LeaderService {

    @Autowired
    private LeaderMapper leaderMapper;
    @Autowired
    private AdjustDormitoryMapper AdjustDormitoryMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private IndisciplineMapper indisciplineMapper;

    public List<Leader> selectLeaderByNameAndAccount(Dot dot) {
        QueryWrapper<Leader> wrapper = new QueryWrapper<Leader>()
                .like("name", dot.getData().get("leaderName"))
                .like("account", dot.getData().get("leaderAccount"));
        Page<Leader> leaderPage = new Page<>(dot.getPageNo(), dot.getPageSize());
        IPage<Leader> leaderIPage = this.baseMapper.selectPage(leaderPage, wrapper);
        return leaderIPage.getRecords();
    }
    public List<RoomAdjustForm> getAllAdjustApplication(Dot dot){
        return leaderMapper.getAllAdjustApplication(dot);
    }
    public  List<Building> getBuildingsByGender(Long ID){
        QueryWrapper<AdjustDormitory> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("ID",ID);
        Long studentID = AdjustDormitoryMapper.selectOne(wrapper1).getStudent();
        String gender = studentMapper.selectOne(new QueryWrapper<Student>().eq("ID",studentID)).getGender();
        List<Building> buildings = buildingMapper.getBuildingByGender(gender);
        return buildings;
    }
    public boolean updateAdjustByLeader(Long id, Long buildingID){
        UpdateWrapper<AdjustDormitory> wrapper = new UpdateWrapper<AdjustDormitory>()
                .eq("ID",id)
                .set("in_building",buildingID);
        return AdjustDormitoryMapper.update(null,wrapper) > 0;
    }
    public boolean approveAdjustByLeader(Long id, Long leaderID, String state){
        UpdateWrapper<AdjustDormitory> wrapper = new UpdateWrapper<AdjustDormitory>();
        wrapper.eq("ID",id)
                .set("state",state)
                .set("leader",leaderID);
        boolean flag = AdjustDormitoryMapper.update(null,wrapper) > 0;
        if (flag) {
            if(state.equals("已完成")){
                UpdateWrapper<Student> wrapper1 = new UpdateWrapper<>();
                wrapper1.eq("ID",AdjustDormitoryMapper.selectOne(wrapper).getStudent())
                        .set("room",AdjustDormitoryMapper.selectOne(wrapper).getInRoom());
                return studentMapper.update(null,wrapper1) > 0;
            }
        }
        return flag;
    }
    public List<RoomAdjustForm> getCheckOutApplication(Dot dot){
        return leaderMapper.getCheckOutApplication(dot);
    }
    public  boolean UpdateCheckOutApplication(Long id, Long leaderID){
        UpdateWrapper<AdjustDormitory> wrapper = new UpdateWrapper<>();
        wrapper.eq("ID",id)
                .set("state","已完成")
                .set("leader",leaderID);
        boolean flag =AdjustDormitoryMapper.update(null,wrapper) > 0;
        if(flag){
            UpdateWrapper<Student> wrapper1 = new UpdateWrapper<>();
            wrapper1.eq("ID",AdjustDormitoryMapper.selectOne(wrapper).getStudent())
                    .set("room",null);
            return studentMapper.update(null,wrapper1) > 0;
        }
        return false;
    }
    public List<Indiscipline> getIndisciplineAudit(Dot dot){
        return leaderMapper.getIndisciplineAudit(dot);
    }
    public boolean updateIndisciplineAudit(Long id, Long leaderID, int score){
        UpdateWrapper<Indiscipline> wrapper = new UpdateWrapper<>();
        wrapper.eq("ID",id)
                .set("state","已完成")
                .set("leader",leaderID)
                .set("grade",score);
        return indisciplineMapper.update(null,wrapper)>0;
    }
}
